package edu.kit.ksri.db.mturk.utils;

import java.io.File;
import java.io.PrintWriter;

/**
 * Copyright (c) 2010-2011 Karlsruhe Institute of Technology (KIT), Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 * --------------------------------------------------------------------------
 * 
 * Software: CSP/WMV tool for dynamic quality management of microtasks
 * http://www.ksri.kit.edu/Upload/Users/PeopleClouds/HCOMP2011_An_extendable_toolkit_for_managing_quality_of_human-based_eServices.pdf
 * 
 * Organization: Karlsruhe Institute of Technology (KIT), Germany
 * http://www.kit.edu
 * 
 * Project: PeopleClouds project of the Karlsruhe Service Research Institute(KSRI)
 * http://www.ksri.kit.edu/Default.aspx?PageId=818
 * 
 * Contributing authors: David Bermbach, Sandra Rath, Pascal Wichmann, Robert Kern
 */
public class CSVUtils {

	/**
	 * 
	 * 
	 * @param data
	 * @return a csv representation of the parameter data which can also be
	 *         written into a file afterwards.
	 */
	public static String toCSV(String[][] data) {
		String output = "";
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				output += "\"" + data[i][j] + "\";";
			}
			output += "\n";
		}
		return output.trim();

	}

	/**
	 * writes a csv representation of the parameter "data" into the file "file".
	 * 
	 * 
	 * @param data
	 * @param file
	 * @return true if successful, false otherwise
	 */
	public static boolean toCSVFile(String[][] data, String file) {
		String s = toCSV(data);
		try {
			PrintWriter pw = new PrintWriter(new File(file));
			pw.print(s);
			pw.close();
			return true;
		} catch (Exception e) {
			return false;
		}

	}



	/**
	 * 
	 * 
	 * @param data
	 * @param captions
	 *            captions which are not yet included in data
	 * @return a csv representation of the parameter data which can also be
	 *         written into a file afterwards.
	 */
	public static String toCSV(String[][] data, String[] captions) {
		return toCSV(combineStringArraysforToCSVWithCaptions(data, captions));
	}

	/**
	 * helper method for String toCSV(String [][]data, String [] captions)
	 * 
	 * @param data
	 * @param captions
	 */
	public static String[][] combineStringArraysforToCSVWithCaptions(
			String[][] data, String[] captions) {
		String[][] temp = new String[data.length + 1][data[0].length];
		temp[0] = captions;
		for (int i = 0; i < data.length; i++) {
			temp[i + 1] = data[i];
		}
		return temp;
	}

	/**
	 * writes a csv representation of the parameter "data" into the file "file".
	 * 
	 * 
	 * @param data
	 * @param captions
	 *            captions which are not yet included in data
	 * @param file
	 * @return true if successful, false otherwise
	 */
	public static boolean toCSVFile(String[][] data, String[] captions,
			String file) {
		return toCSVFile(
				combineStringArraysforToCSVWithCaptions(data, captions), file);
	}

	/**
	 * transforms a 2D array to a 2D String array.
	 * 
	 * @param <T>
	 *            type of the array
	 * @param array
	 * @return
	 */
	public static <T> String[][] toStringArray(T[][] array) {
		if (array == null)
			return null;
		String[][] res = new String[array.length][array[0].length];
		int counter = 0;
		for (T[] t : array) {
			if (t == null)
				continue;
			res[counter] = new String[t.length];
			int counter2 = 0;
			for (T tt : t) {
				res[counter][counter2++] = "" + tt;
			}
			counter++;
		}

		return res;

	}

	/**
	 * transforms an array to a String array.
	 * 
	 * @param <T>
	 *            type of the array
	 * @param array
	 * @return
	 */
	public static <T> String[] toStringArray(T[] array) {
		if (array == null)
			return null;
		String[] res = new String[array.length];
		int counter = 0;
		for (T t : array) {
			if (t == null) {
				res[counter] = "";
				continue;
			}
			res[counter] = "" + t;
			counter++;
		}

		return res;

	}

	/**
	 * transforms a 1D string array to a 2d string array without affecting the
	 * data
	 * 
	 * @param oneDim
	 * @return
	 */
	public static String[][] oneDimToTwoDim(String[] oneDim) {
		String[][] temp = new String[1][1];
		temp[0] = oneDim;
		return temp;
	}

}
